<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AdminUser;
use App\Admin\Repositories\OrgDepartment;
use App\Admin\Repositories\OrgTeam;
use App\Admin\Repositories\OrgTeamOwner;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;

class OrgTeamController extends AdminController
{
    public function index(Content $content)
    {
        return $content
            ->header('小组管理')
            ->description('列表')
            ->body($this->grid());
    }

    protected function grid()
    {
        return new Grid(new OrgTeam(['department', 'owners.username']), function (Grid $grid) {
            $grid->model()->orderBy('team_id', 'desc');
            $grid->column('team_id', '小组ID');
            $grid->column('name', '小组名称');
            $grid->column('department.name', '所属部门')->label('success');
            $grid->owners('负责人')->display(function($users){
                $user_str = [];
                foreach ($users as $user){
                    $user_str[] = '<span class="badge" style="background:#5c6bc6">'.$user['username']['name'].'</span>';
                }
                return implode(' ', $user_str);
            });
            $grid->column('created_at', '添加时间');
            $grid->column('updated_at', '更新时间')->sortable();
            $grid->disableRowSelector();
            $grid->disableViewButton();
            $grid->setActionClass(Grid\Displayers\Actions::class);
            $grid->enableDialogCreate();
            $grid->showQuickEditButton();
            $grid->disableEditButton();
        });
    }

    protected function form()
    {
        return Form::make(new OrgTeam(['department', 'owners']), function (Form $form) {
            if ($form->isEditing()) {
                $user_list = app(OrgTeamOwner::class)->getOwnerListByTeamId($form->getKey());

                $form->text('name')
                    ->maxLength(15, '最长不超过15个字符')
                    ->required();
                $form->select('department_id', '所属部门')->options(app(OrgDepartment::class)->getOptionList())->required();
                $form->multipleSelect('owner_uid', '选择负责人')
                    ->options(app(AdminUser::class)->getOptionList())
                    ->customFormat(function ($v) {
                        if(!$v) return [];
                        return array_column($v, 'id');
                    })
                    ->default(collect($user_list)->pluck('owner_uid')->toArray(), true)
                    ->required();
            }else {
                $form->text('name')->maxLength(15, '最长不超过15个字符')->required();
                $form->select('department_id', '所属部门')->options(app(OrgDepartment::class)->getOptionList())->required();
                $form->multipleSelect('owner_uid', '选择负责人')->options(app(AdminUser::class)->getOptionList())->required();
            }
        });
    }

    public function store()
    {
        $name = request('name');
        $belongs_department_id = request('department_id');
        $owner_list = request('owner_uid');
        if (app(OrgTeam::class)->add($name, $belongs_department_id, $owner_list)){
            return response()->json([
                'message' => '添加成功',
                'status' => true,
                'redirect' => ''
            ]);
        }
        return response()->json([
            'message' => '添加失败',
            'status' => false,
            'redirect' => ''
        ]);
    }

    public function update($id)
    {
        $team_id = $id;
        $name = request('name');
        $belongs_department_id = request('department_id');
        $owner_list = request('owner_uid');
        if (app(OrgTeam::class)->updt($team_id, $name, $belongs_department_id, $owner_list)){
            return response()->json([
                'message' => '编辑成功',
                'status' => true,
                'redirect' => ''
            ]);
        }
        return response()->json([
            'message' => '编辑失败',
            'status' => false,
            'redirect' => ''
        ]);
    }

    public function destroy($id)
    {
        if (app(OrgTeam::class)->del($id)){
            return response()->json([
                'message' => '删除成功',
                'status' => true,
                'redirect' => ''
            ]);
        }
        return response()->json([
            'message' => '删除失败',
            'status' => false,
            'redirect' => ''
        ]);
    }
}
